#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "sms.h"

#define DIANDU_LAYOUT_NAME "layout-diandu"

#define ANIMATE_STOP     1
#define ANIMATE_RESTART  2
#define ANIMATE_CONTINUE 3

typedef enum _DIANDU_ICON_TYPE{
	DIANDU_ICON_TYPE_NONE,
	DIANDU_ICON_TYPE_SHOW_SUB,
	DIANDU_ICON_TYPE_EXEC,
	DIANDU_ICON_TYPE_LAYOUT
}DIANDU_ICON_TYPE;


typedef struct _Icon
{
	char *name;

      DIANDU_ICON_TYPE type;

	GooCanvasItem *icon;
	GooCanvasItem *icon_a;

	LAYOUT_WIN_ID id;
	char *layout_property[2];

	char *cmd;
	char **argv;
	int argc;

	int size;
	int asize;
	int sub_count;
	int sub_cur;
	int sicon_jump_id;

	gboolean is_sub;

	struct _Icon *sub_icons;
	struct _Icon *sicon_cur;
	struct _Icon *sicon_jump;

	struct _Icon *next;
	struct _Icon *prv;

}Icon;

typedef struct _Title{	
	int x;
	int y;
	int size;
	char *color;
	char *font;
	GooCanvasItem *text;
}Title;


typedef struct _DianduLayout
{
	GooCanvasItem *bg;

	int micon_count;

	Icon *icons;
	Icon *micon_cur;
	Icon *micon_jump;

	int micon_jump_id;

	Animate *animates;
	int animate_count;

	int *micon_pos[2];
	Exec *exec;
	Title title;
}DianduLayout;

#define DIANDU_LAYOUT_ANI_DURATION 300
#define DIANDU_LAYOUT_ANI_STEP	50

static void diandu_icon_ani_end(GooCanvasItem *item,gboolean stopped,gpointer user_data) 
{
	printf("~~~~%s\n" ,__FUNCTION__);
	double x,y,s,r;
	Layout *layout=user_data;
	DianduLayout *dd_layout = layout->private ;
	int **mpos=dd_layout->micon_pos;
	int p=dd_layout->micon_cur->size;
	SMS *sms = layout->sms ;

	if(item==dd_layout->micon_cur->icon)
	{
		int sp=(dd_layout->micon_cur->size-dd_layout->micon_cur->size)/2;
		g_object_set(dd_layout->micon_cur->icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		g_object_set(dd_layout->micon_cur->icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

		goo_canvas_item_set_simple_transform(dd_layout->micon_cur->icon_a,mpos[0][0]-sp,mpos[1][0]-sp,1,0);

		g_object_set(dd_layout->title.text,"text",dd_layout->micon_cur->name,NULL);
		sms->busy = FALSE ;	
	
		return ;		
	}

	if(dd_layout->micon_jump && item==dd_layout->micon_jump->icon)
	{
		GooCanvasItem *jitem;
		int id=dd_layout->micon_jump_id;
		int xx;
		if(id==2){
			xx=mpos[0][id]+p;
			jitem=dd_layout->micon_cur->next->next->icon;
		}
		else{
			xx=mpos[0][id];
			jitem=dd_layout->micon_cur->prv->prv->icon;
		}
		g_object_set(item,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
		g_object_set(jitem,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		goo_canvas_item_set_simple_transform(jitem,xx,mpos[1][id]+p,0.1,0);
		goo_canvas_item_animate(jitem,mpos[0][id],mpos[1][id],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION/2,DIANDU_LAYOUT_ANI_STEP,
				GOO_CANVAS_ANIMATE_FREEZE);
		dd_layout->micon_jump=NULL;
		return;
	}

	
}

static gboolean diandu_layout_load_bg(xmlNode *a_node,Layout *layout)
{
	DianduLayout *dd_layout = layout->private ;
	GdkPixbuf*  pixbuf;
	xmlChar * val;
	char pic[256];

	if(dd_layout->bg||!a_node)
		return FALSE;

	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(!val)
		return FALSE;
	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);

	pixbuf=sms_load_pic(pic,800,480);
	if(!pixbuf)
		return FALSE;

	dd_layout->bg = goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);	

	GooCanvasItem *wave ,*seaweed1 ,*seaweed2 ,*seaweed3 ,*ship;

	sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
			,layout->name,"Ship.png");
	GdkPixbuf  *pixbuf_ship=gdk_pixbuf_new_from_file(pic ,NULL);
	ship = goo_canvas_image_new(layout->cavas ,pixbuf_ship ,0 ,0 ,NULL);
	goo_canvas_item_set_simple_transform(ship ,174 ,0 ,1 ,0);

	/*sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default"
			,layout->name,"Wave0.png");
	GdkPixbuf  *pixbuf_wave=gdk_pixbuf_new_from_file(pic ,NULL);
	wave = goo_canvas_image_new(layout->cavas ,pixbuf_wave ,0 ,0 ,NULL);
	//goo_canvas_item_set_simple_transform(wave ,174 ,0 ,1 ,0);
	goo_canvas_item_set_simple_transform(wave ,-800 ,90 ,1 ,0);
	goo_canvas_item_animate(wave ,800 ,0 ,1 ,0 ,FALSE ,40000 ,100 ,
			GOO_CANVAS_ANIMATE_BOUNCE);

	sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
			,layout->name,"Seaweed1.png");
	GdkPixbuf *pixbuf_sea1 =gdk_pixbuf_new_from_file(pic,NULL);
	seaweed1 = goo_canvas_image_new(layout->cavas ,pixbuf_sea1 ,0 ,0 ,NULL);
	goo_canvas_item_set_simple_transform(seaweed1 ,117 ,400 ,1 ,0);
	goo_canvas_item_animate(seaweed1 ,167 ,360 ,2 ,0 ,TRUE ,5000 ,80 ,
			GOO_CANVAS_ANIMATE_BOUNCE);

	sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
			,layout->name,"Seaweed.png");
	GdkPixbuf *pixbuf_sea2 =gdk_pixbuf_new_from_file(pic ,NULL);
	seaweed2 = goo_canvas_image_new(layout->cavas ,pixbuf_sea2 ,0 ,0 ,NULL);
	goo_canvas_item_set_simple_transform(seaweed2 ,464 ,451 ,1 ,0);
	goo_canvas_item_animate(seaweed2 ,400 ,390 ,2 ,0 ,TRUE ,5000 ,80 ,
			GOO_CANVAS_ANIMATE_BOUNCE);

	sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
			,layout->name,"Seaweed1.png");
	GdkPixbuf *pixbuf_sea3 =gdk_pixbuf_new_from_file(pic,NULL);
	seaweed3 = goo_canvas_image_new(layout->cavas ,pixbuf_sea3 ,0 ,0 ,NULL);
	goo_canvas_item_set_simple_transform(seaweed3 ,640 ,449 ,1 ,0);
	goo_canvas_item_animate(seaweed3 ,640 ,380 ,2 ,0 ,TRUE ,5000 ,80 ,
			GOO_CANVAS_ANIMATE_BOUNCE);

	g_object_unref(pixbuf_sea1);
	g_object_unref(pixbuf_sea2);
	g_object_unref(pixbuf_sea3);
	g_object_unref(pixbuf_wave);*/
	g_object_unref(pixbuf_ship);

	
}


static void diandu_layout_destroy_icon(Icon *icon)
{
	Icon *nicon,*picon;
	int i;

	if(!icon)
		return;

	nicon=icon->next;
	picon=icon->prv;
	if(nicon&&picon)
	{
		nicon->prv=picon;
		picon->next=nicon;
	}

	if(icon->name)
		free(icon->name);
	if(icon->icon)
		g_object_unref(icon->icon);
	if(icon->icon_a)
		g_object_unref(icon->icon_a);
	if(icon->layout_property[0])
		free(icon->layout_property[0]);
	if(icon->layout_property[1])
		free(icon->layout_property[1]);
	if(icon->cmd)
		free(icon->cmd);

	for(i=0;i<icon->argc;i++)
	{
		if(icon->argv[i])
			free(icon->argv[i]);
	}

	nicon=icon->sub_icons;
	for(i=0;i<icon->sub_count;i++)
	{	
		diandu_layout_destroy_icon(nicon);
		nicon=nicon->next;
	}
}

static Icon * diandu_layout_create_icon(xmlNode *a_node,Layout *layout)
{
	DianduLayout *dd_layout = layout->private ;
	SMS *sms=layout->sms;

	int id;
	GdkPixbuf*  pixbuf;
	xmlChar * val;
	char pic[256];

	int d=0;

	Icon *icon=(Icon *)g_malloc0(sizeof(Icon)+1);
	if(!icon)
		goto err;

	val = xmlGetProp (a_node,(xmlChar*)"name");
	if(!val)
		goto err;
	icon->name=g_strdup(val);
	icon->size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
	icon->asize=sms_get_xml_int(a_node,(xmlChar*)"asize",icon->size);

	//icon
	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(!val)
		goto err;
	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
	pixbuf=sms_load_pic(pic,icon->size,icon->size);
	if(!pixbuf)
		goto err;	
	icon->icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);	
	g_object_set(icon->icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	g_signal_connect(icon->icon,"animation-finished",(GCallback) diandu_icon_ani_end,layout);

	//icon ani
	val = xmlGetProp (a_node,(xmlChar*)"apic");
	if(!val)
		goto err;
	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
	pixbuf=sms_load_pic(pic,icon->asize,icon->asize);
	if(!pixbuf)
		goto err;
	icon->icon_a=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);
	g_object_set(icon->icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

	//is exec?
	if(icon->type==DIANDU_ICON_TYPE_NONE)
	{
		val = xmlGetProp (a_node,(xmlChar*)"exec");
		if(val)
		{
			icon->type=DIANDU_ICON_TYPE_EXEC;
			icon->cmd=g_strdup(val);
			printf("cmd=%s\n" ,icon->cmd);

			//val = xmlGetProp (a_node,(xmlChar*)"books_path");
			//if(val)
			//{
				//icon_get_exec_arg(val,icon);
			//}
		}
	}


	return icon;
err:
	fprintf(stderr,"%s:error\n",__FUNCTION__);
	if(icon)
		diandu_layout_destroy_icon(icon);
	return NULL;

}

static gboolean diandu_layout_load_icons(xmlNode *a_node,Layout *layout)
{
	DianduLayout *dd_layout = layout->private ;
	xmlNode *cur_node = NULL;
	Icon * icon;

	if(!a_node)
		return FALSE;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next){
		if (cur_node->type == XML_ELEMENT_NODE)
		{
			if(strcmp(cur_node->name,"icon")==0)
			{
				icon=diandu_layout_create_icon(cur_node,layout);
				if(icon)
				{
					dd_layout->micon_count++;
					if(dd_layout->icons)
					{
						Icon *picon;
						picon=dd_layout->icons->prv;
						picon->next=icon;
						icon->prv=picon;
						dd_layout->icons->prv=icon;
						icon->next=dd_layout->icons;							
					}
					else
					{
						dd_layout->icons=icon;
						icon->next=icon;
						icon->prv=icon;						
					}
				}
			}
		}
	}

}

static gboolean diandu_layout_load_title(xmlNode *a_node,Layout *layout)
{
	DianduLayout *dd_layout = layout->private;
	Title * title=&(dd_layout->title);
	xmlNode *cur_node = a_node;
	xmlChar * val;
	char font[64];
	char pic[256];

	if(!a_node)
		goto err;

	GooCanvasItem *logo;
	sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" ,layout->name ,"logo.png");
	GdkPixbuf  *pixbuf_logo = gdk_pixbuf_new_from_file_at_size(pic ,120 ,138 ,NULL);
	logo = goo_canvas_image_new(layout->cavas ,pixbuf_logo ,0 ,0 ,NULL);
	goo_canvas_item_set_simple_transform(logo ,656 ,19 ,1 ,0);

	title->x=sms_get_xml_int(cur_node,(xmlChar*)"x",0);
	title->y=sms_get_xml_int(cur_node,(xmlChar*)"y",0);
	title->size=sms_get_xml_int(cur_node,(xmlChar*)"size",10);

	val = xmlGetProp (cur_node,(xmlChar*)"color");
	if(val)
	{
		title->color=g_strdup(val);
	}
	val = xmlGetProp (cur_node,(xmlChar*)"font");
	if(val)
	{
		title->font=g_strdup(val);
	}

	sprintf(font ,"%s %d",title->font ,title->size);
	title->text=goo_canvas_text_new(layout->cavas,"",
			title->x,title->y,-1,GTK_ANCHOR_CENTER,
			"font", font,NULL);
	if(title->color)
		g_object_set(title->text,"fill_color",title->color,NULL);


	return TRUE;
err:
	return FALSE;
}

static void diandu_layout_create_pos(xmlNode *a_node,int count,int *pos[])
{
	int i,points;
	char key[16];
	xmlChar * val;

	points=sms_get_xml_int (a_node,(xmlChar*)"points",0);
      printf("points=%d\n" ,points);

	if(count<points)
		return ;

	*pos=(int *)g_malloc0(sizeof(int)*count+1);
	*(pos+1)=(int *)g_malloc0(sizeof(int)*count+1);

	for(i=0;i<count;i++)
	{
		sprintf(key,"p%d_x",i+1);
		pos[0][i] = sms_get_xml_int (a_node,(xmlChar*)key,0);
		sprintf(key,"p%d_y",i+1);
		pos[1][i] = sms_get_xml_int (a_node,(xmlChar*)key,0);
	}
	
	return;

err:
	if(pos)
		free(pos);
}

/*
 *hjq add for stop animate
 */

void dd_animation_finished (GooCanvasItem *item, gboolean stopped, gpointer user_data)
{
	printf("~~~%s\n" ,__FUNCTION__);
	DianduLayout *layout = (DianduLayout*) user_data;
	if (stopped)
	{
		g_print("manually stop.\n");
	}
	else
	{
		g_print("auto stop.\n");
		//goo_canvas_item_set_simple_transform(item , -100 ,90 ,1 ,0);
		//goo_canvas_item_animate(item, 0, 90, 1, 0, TRUE, 4000, 100, GOO_CANVAS_ANIMATE_RESET);
		dd_animate_control (layout, ANIMATE_RESTART);
	}
}

gboolean dd_timeout_callback(gpointer user_data)
{
	printf("~~~%s\n" ,__FUNCTION__);
	DianduLayout *layout = (DianduLayout*) user_data;
	dd_animate_control (layout, ANIMATE_CONTINUE);
	return FALSE;
}

/*
 * 1: stop
 * 2: restart
 * 3: continue
 * */

void dd_animate_control (DianduLayout *layout, int control)
{
	printf("~~~%s\n" ,__FUNCTION__);
	int i;
	Animate *animate;
	for (i=0; i<layout->animate_count; i++)
	{
		animate = layout->animates;
		switch (control)
		{
		case ANIMATE_STOP:
			animate_stop(animate);
			break;
		case ANIMATE_RESTART:
			animate_restart(animate);
			break;
		case ANIMATE_CONTINUE:
			animate_continue (animate);
			break;
		default:
			break;
		}
		animate = animate->next;
	}
}

#define ANIMATE_FREEZE   1
#define ANIMATE_RESET    2
#define ANIMATE_RESTART  3
#define ANIMATE_BOUNCE   4

static Animate * diandu_layout_create_animate(xmlNode *a_node,Layout *layout)
{
	printf("~~~%s\n" ,__FUNCTION__);

	DianduLayout *dd_layout=layout->private;
	Animate *animate;
	xmlChar * val;
	char pic[256];

	gdouble start_x;
	gdouble start_y;
	gdouble stop_x;
	gdouble stop_y;

	gdouble init_scale;
	gdouble init_rotation;
	gdouble animate_scale;
	gdouble animate_rotation;
	gboolean isAbsolute;
	gint total_time;
	gint step_time;
	GooCanvasAnimateType type;

	start_x = sms_get_xml_double (a_node, (xmlChar*)"start_x", 0);
	start_y = sms_get_xml_double (a_node, (xmlChar*)"start_y", 0);
	stop_x = sms_get_xml_double (a_node, (xmlChar*)"stop_x", 0);
	stop_y = sms_get_xml_double (a_node, (xmlChar*)"stop_y", 0);
	init_scale = sms_get_xml_double (a_node, (xmlChar*)"init_scale", 0);
	init_rotation = sms_get_xml_double (a_node, (xmlChar*)"init_rotation", 0);
	animate_scale = sms_get_xml_double (a_node, (xmlChar*)"animate_scale", 0);
	animate_rotation = sms_get_xml_double (a_node, (xmlChar*)"animate_rotation", 0);
	isAbsolute = sms_get_xml_boolean (a_node, (xmlChar*)"isAbsolute");
	total_time = sms_get_xml_int (a_node, (xmlChar*)"total_time", 0);
	step_time = sms_get_xml_int (a_node, (xmlChar*)"step_time", 0);

	switch (sms_get_xml_int (a_node, (xmlChar*)"type", 1))
	{
	case ANIMATE_FREEZE:
		type = GOO_CANVAS_ANIMATE_FREEZE;
		break;
	case ANIMATE_RESET:
		type = GOO_CANVAS_ANIMATE_RESET;
		break;
	case ANIMATE_RESTART:
		type = GOO_CANVAS_ANIMATE_RESTART;
		break;
	case ANIMATE_BOUNCE:
		type = GOO_CANVAS_ANIMATE_BOUNCE;
		break;
	default: break;
	}
	g_print ("startx: %f, starty: %f, stopx: %f, stopy: %f,init_scale: %f, animate_scale: %f, animate_rotation: %f, total_time: %d, step_time %d\n",
			  start_x, start_y, stop_x, stop_y, init_scale, animate_scale, total_time, step_time);

	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(!val)
		goto err;
	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
	g_print ("pic path: %s", pic);

	animate = animate_new (layout->cavas, pic, start_x, start_y, stop_x, stop_y, init_scale, init_rotation,
			     animate_scale, animate_rotation, isAbsolute, total_time, step_time, type);
	g_signal_connect (G_OBJECT (animate->cavas), "animation-finished", G_CALLBACK (dd_animation_finished), dd_layout);
	return animate;
err:
	fprintf(stderr,"%s:error\n",__FUNCTION__);
	return NULL;
}

static gboolean diandu_layout_load_animates(xmlNode *a_node, Layout *layout)
{
	DianduLayout *dd_layout=layout->private;

	xmlNode *cur_node = NULL;
	Animate * animate;

	if(!a_node)
		return FALSE;
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"animate")==0)
			{
				animate = diandu_layout_create_animate(cur_node, layout);
				if(animate)
				{
					dd_layout->animate_count++;
					if(dd_layout->animates)
					{
						dd_layout->animates->next = animate;
					}
					else
					{
						dd_layout->animates = animate;
					}
				}
			}
		}
	}
	return TRUE;
}

static gboolean diandu_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
	DianduLayout *dd_layout = layout->private;
	xmlNode *cur_node = NULL;
	xmlNode *bg_node = NULL;
	xmlNode *icon_node = NULL;
	xmlNode *title_node = NULL;
	xmlNode *animate_node = NULL;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next){
		if (cur_node->type == XML_ELEMENT_NODE){
			printf("node type: Element, name: %s\n", cur_node->name);
			if(strcmp(cur_node->name,"bg")==0)
				bg_node=cur_node;
			else if(strcmp(cur_node->name,"icons")==0)
				icon_node=cur_node;
			else if(strcmp(cur_node->name,"title")==0)
				title_node=cur_node;
			else if(strcmp(cur_node->name,"micon_pos")==0)
				diandu_layout_create_pos(cur_node,5,dd_layout->micon_pos);
			else if(strcmp(cur_node->name,"animates")==0)
				animate_node = cur_node;
		}
	}

	if(!bg_node||!icon_node||!title_node||!animate_node){
		return FALSE;
	}
	diandu_layout_load_bg(bg_node,layout);
	diandu_layout_load_animates (animate_node->children, layout);
	diandu_layout_load_icons(icon_node->children,layout);
	diandu_layout_load_title(title_node,layout);

	if(dd_layout->micon_count>=5&&dd_layout->micon_pos)
		return TRUE;

	return FALSE;

}

gboolean diandu_layout_load_config(Layout *layout)
{
	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	DianduLayout *dd_layout = layout->private;
	char conf[256];

      printf("layout->name=%s\n" ,layout->name);
	sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
      printf("conf=%s\n" ,conf);
	doc = xmlReadFile(conf, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf);
		return ret;
	}

	root_element = xmlDocGetRootElement(doc);
	
	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
			ret=diandu_layout_travel_xml(cur_node->children,layout);
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();
	
	return ret;
}

static void diandu_layout_reset_icon(Icon * icon,int x,int y,int **mpos)
{
    goo_canvas_item_translate(icon->icon,x,y);
    g_object_set(icon->icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

}


static void diandu_layout_reset_icons(Layout *layout)
{
    printf("~~~%s\n" ,__FUNCTION__);
    DianduLayout *dd_layout = layout->private;
    Icon * icon=dd_layout->icons;
    int **mpos=dd_layout->micon_pos;

    diandu_layout_reset_icon(icon,mpos[0][0],mpos[1][0],mpos);
    goo_canvas_item_translate(icon->icon_a,mpos[0][0],mpos[1][0]);  
    g_object_set(icon->icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    g_object_set(icon->icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    g_object_set(dd_layout->title.text,"text",icon->name,NULL);
    icon=icon->next;
    diandu_layout_reset_icon(icon,mpos[0][1],mpos[1][1],mpos);

    icon=icon->next;
    diandu_layout_reset_icon(icon,mpos[0][2],mpos[1][2],mpos);

    icon=dd_layout->icons->prv;
    diandu_layout_reset_icon(icon,mpos[0][3],mpos[1][3],mpos);

    icon=icon->prv;
    diandu_layout_reset_icon(icon,mpos[0][4],mpos[1][4],mpos);

    dd_layout->micon_cur=dd_layout->icons;  



}

gboolean diandu_layout_init(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	DianduLayout *dd_layout = (DianduLayout *)g_malloc0(sizeof(DianduLayout)+1);
	layout->private = (void *)dd_layout ;

	layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

	if(!diandu_layout_load_config(layout))
	{
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		//return FALSE;//exit(-1);
	}

	diandu_layout_reset_icons(layout);

	return TRUE;
}

gboolean diandu_layout_clear(Layout *layout,gpointer data)
{
	DianduLayout *dd_layout;
	int i;

	printf("call %s\n",__FUNCTION__);

	dd_layout=layout->private;

	g_object_unref(dd_layout->bg);	

	g_free(dd_layout);
	return TRUE;
}

static void diandu_layout_move_micon(Layout *layout,gboolean is_left)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	DianduLayout *dd_layout=layout->private;
	Icon *icon;
	int **mpos=dd_layout->micon_pos;
	int p=dd_layout->micon_cur->size;
	icon=dd_layout->micon_cur;

	g_object_set(icon->icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	g_object_set(icon->icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

	if(is_left)
	{		
		goo_canvas_item_animate(icon->icon,mpos[0][1],mpos[1][1],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}
	else
	{
		goo_canvas_item_animate(icon->icon,mpos[0][3],mpos[1][3],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}

	icon=icon->next;	
	if(is_left)
	{		
		goo_canvas_item_animate(icon->icon,mpos[0][2],mpos[1][2],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}
	else
	{
		goo_canvas_item_animate(icon->icon,mpos[0][0],mpos[1][0],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
		//set end animate
	}

	icon=icon->next;	
	if(is_left)
	{		
		goo_canvas_item_animate(icon->icon,mpos[0][2]+p,mpos[1][2]+p,0.1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
		//set end animate
		dd_layout->micon_jump=icon;
		dd_layout->micon_jump_id=4;
	}
	else
	{
		goo_canvas_item_animate(icon->icon,mpos[0][1],mpos[1][1],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}

	icon=dd_layout->micon_cur->prv;	
	if(is_left)
	{		
		goo_canvas_item_animate(icon->icon,mpos[0][0],mpos[1][0],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
		//set end animate
	}
	else
	{
		goo_canvas_item_animate(icon->icon,mpos[0][4],mpos[1][4],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}

	icon=icon->prv;	
	if(is_left)
	{		
		goo_canvas_item_animate(icon->icon,mpos[0][3],mpos[1][3],1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
	}
	else
	{
		goo_canvas_item_animate(icon->icon,mpos[0][4]+p,mpos[1][4]+p,0.1,0,TRUE,
				DIANDU_LAYOUT_ANI_DURATION,DIANDU_LAYOUT_ANI_STEP,GOO_CANVAS_ANIMATE_FREEZE);
		dd_layout->micon_jump=icon;
		dd_layout->micon_jump_id=2;
	}

	if(is_left)
		dd_layout->micon_cur=dd_layout->micon_cur->prv;
	else
		dd_layout->micon_cur=dd_layout->micon_cur->next;
}

gboolean diandu_layout_key_press(Layout * layout,GdkEventKey *event)
{
	DianduLayout *dd_layout=layout->private;

	if(event->keyval==GDK_Left)
	{
		dd_animate_control (dd_layout, ANIMATE_STOP);
		g_timeout_add(300, (GSourceFunc) dd_timeout_callback, dd_layout);
		diandu_layout_move_micon(layout,TRUE);
		return TRUE;
	}
	else if(event->keyval==GDK_Right)
	{
		dd_animate_control (dd_layout, ANIMATE_STOP);
		g_timeout_add(300, (GSourceFunc) dd_timeout_callback, dd_layout);
		diandu_layout_move_micon(layout,FALSE);
		return TRUE;
	}
	else if(event->keyval==GDK_Escape){		
		sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
		return TRUE;
	}
       else if(event->keyval==GDK_Return){
             Icon *icon;
             icon=dd_layout->micon_cur;
             if(icon->type==DIANDU_ICON_TYPE_EXEC){
                    printf("exec=%s\n" ,icon->cmd);
                    system(icon->cmd);
                    //dd_layout->exec=exec_create("gedit" ,2 ,argv ,FALSE ,main_layout_exec_finish,main_layout);
		   }
             }
	
	return FALSE;
}


Layout *diandu_layout_new(GooCanvasItem * parent,gpointer sms)
{
	Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN,LAYOUT_WIN_DIANDU,
							   DIANDU_LAYOUT_NAME,sms);

	layout_set_func(layout,"init",diandu_layout_init);
	layout_set_func(layout,"clear",diandu_layout_clear);
	layout_set_func(layout,"key-press",diandu_layout_key_press);
	return layout;
}
